﻿using FishNet.Authenticating;
using FishNet.Connection;
using FishNet.Managing;
using System;
using System.Collections;
using UnityEngine;

namespace VCity
{
    public class NetServerAuthenticator : Authenticator
    {
        public override event Action<NetworkConnection, bool> OnAuthenticationResult;

        public override void InitializeOnce(NetworkManager networkManager)
        {
            base.InitializeOnce(networkManager);

            //Listen broadcast from client.
            networkManager.ServerManager.RegisterBroadcast<NetLoginData>(OnLoginRequest, false);
        }

        //public override void OnRemoteConnection(NetworkConnection connection)
        //{
        //    base.NetworkManager.ClientManager.Broadcast<>
        //    OnAuthenticationResult?.Invoke(connection, true);
        //}

        void OnLoginRequest(NetworkConnection connection, NetLoginData data)
        {
            // Send login info back to client
            //base.NetworkManager.ServerManager.Broadcast(connection, data, false);
            OnAuthenticationResult?.Invoke(connection, true);

            // Spawn
            NetServer.Instance.OnLogin(connection, data);
        }
    }
}